package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.entity.sysLog.SysLogLogin;
import com.randi.system.domain.params.sysLog.SysLogLoginParam;
import com.randi.system.service.sysLog.ISysLogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/log/login")
@Api(tags = "登录日志")
public class SysLogLoginController {

    @Resource
    private ISysLogLoginService iSysLogLoginService;

    /**
     * 分页获取列表
     * @param sysLogLoginParam 检索参数
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:login:list')")
    public Result selectList(@RequestBody SysLogLoginParam sysLogLoginParam) {
        PageHelperUtils.initPage(sysLogLoginParam);
        PageInfo<SysLogLogin> sysLogLogins = iSysLogLoginService.selectPageList(sysLogLoginParam);
        return Result.success().putPage(sysLogLogins);
    }


    /**
     * 查看详情
     *
     * @param infoId infoId
     * @return 结果
     */
    @GetMapping("/detail/{infoId}")
    @ApiOperation(value = "查看详情", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:login:list')")
    public Result detail(@PathVariable Long infoId){
        SysLogLogin sysLogLogin = iSysLogLoginService.selectSysLogLoginById(infoId);
        return Result.success().put(sysLogLogin);
    }

    /**
     * 删除数据
     *
     * @param infoIds infoIds
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:login:delete')")
    @LogAnnotation(module = "登录日志", value = "删除数据", businessType = BusinessType.DELETE)
    public Result deleteBatch(@RequestBody List<Long> infoIds) {
        int status = iSysLogLoginService.deleteBatchSysLogLogin(infoIds);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

}
